package com.zb.mvc.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.entity.base.JsonCommon;
import com.zb.entity.base.JsonForList;
import com.zb.entity.exception.BizException;
import com.zb.entity.system.OmsOperMerchant;
import com.zb.service.base.OperMerchantService;
import com.zb.util.ActionUtils;
import com.zb.util.OmsConstant;

/**
 * 
 * @author huanghao
 *
 */
@Controller
@RequestMapping("/operMechantApi")
public class OperMerchantAction extends BaseController
{


	@Autowired
	private OperMerchantService opermerchantServiceImpl;
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> addOperMerchant(HttpServletRequest request, HttpServletResponse response)
	{
		OmsOperMerchant oom = this.getObjFromRequest(request,true,false);
		String factoryId = request.getParameter("factory_id");
		String oomType = request.getParameter("oomType");
		return opermerchantServiceImpl.addOmsOperMerchant(oom, oomType, factoryId);
	}
	
	@RequestMapping(value="update/{oper_merchant_id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> updateOperMerchant(@PathVariable(value="oper_merchant_id") String oper_merchant_id,
			HttpServletRequest request, HttpServletResponse response)
	{
		OmsOperMerchant oom = this.getObjFromRequest(request, false, false);
		oom.setOper_merchant_id(oper_merchant_id);
		
		String isFactory = request.getParameter("isFactory");
		String factoryId = request.getParameter("factory_id");
		
		return opermerchantServiceImpl.updateOmsOperMerchant(oom,isFactory,factoryId);
	}
	
	@RequestMapping(value="delete/{oper_merchant_id}",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> deleteOperMerchant(@PathVariable(value="oper_merchant_id") String oper_merchant_id,
			HttpServletRequest request, HttpServletResponse response)
	{
		String delCasecade = request.getParameter("delCasecade");
		return opermerchantServiceImpl.deleteOmsOperMerchant(oper_merchant_id, delCasecade);
	}

	@RequestMapping(value="findAllOrPage",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findPageOperMerchant(HttpServletRequest request, HttpServletResponse response)
	{
		return opermerchantServiceImpl.findPageOmsOperMerchant(ActionUtils.buildQueryParam(request, true), this.getObjFromRequest(request, false, true));
	}

	@RequestMapping(value = "syncFromMS")
	@ResponseBody
	public JsonCommon syncFromMS() throws BizException
	{
		opermerchantServiceImpl.sycnMerchantFromMasterServer();
		return new JsonCommon(null, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	@RequestMapping(value="findFactoryId/{oper_merchant_id}",method=RequestMethod.GET)
	@ResponseBody
	public JsonCommon findFactoryId(@PathVariable(value="oper_merchant_id") String oper_merchant_id,
			HttpServletRequest request, HttpServletResponse response)
	{
		String factoryId = opermerchantServiceImpl.getFactoryId(oper_merchant_id);
		JsonCommon jsonCommon = new JsonCommon();
		jsonCommon.setCode(OmsConstant.SuccessCodeEnum.SuccessCode.value());
		jsonCommon.setPojo(factoryId);
		
		return jsonCommon;
	}
	
	private OmsOperMerchant getObjFromRequest(HttpServletRequest request,boolean isCreate,boolean isFind)
	{
		HttpSession session = request.getSession();
		String loginUserId =  (String) session.getAttribute(BaseAction.CommonParam.SESSION_USERID);
		return ActionUtils.getParamInstance(OmsOperMerchant.class, ActionUtils.getParameterMap(request),loginUserId,isCreate,isFind);
	}
}
